package first.endtoend;

import java.util.List;

import org.apache.http.cookie.Cookie;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import first.endtoend.MySyncService.LocalBinder;

public class IntermediateActivity extends Activity{
	
	MySyncService sync_service;
	public static Context context;
	public static List<Cookie> cookies;
	SharedPreferences settings;
	
	
	Boolean mBound;
	//ServiceConnection mConnection;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intermediate);
		getActionBar().show();
		
		
		/**
		 * To start a transaction and tag Beneficiaries 
		 * */
		Button tagBenefBtn = (Button) findViewById(R.id.button_tag);
		tagBenefBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent (IntermediateActivity.this, TagActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
		/**
		 * Manage the SE which is microSD card 
		 * */
		Button microSDCardBtn = (Button) findViewById(R.id.btn_microSD);
		microSDCardBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent (IntermediateActivity.this, IDGoBindingActivity.class);
				startActivity(intent);
				finish();
			}
		});		
		
		
		/**
		 * Synchronize local data with the server  
		 * */
		Button serverSyncBtn = (Button) findViewById(R.id.button_sync_server);
		serverSyncBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent (IntermediateActivity.this, MySyncService.class);
				bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
				
			}
		});
	}
	
//	@Override
//	protected void onStart() {
//		// TODO Auto-generated method stub
//		super.onStart();
//		//Bind to MySyncService
//		Intent intent = new Intent (IntermediateActivity.this, MySyncService.class);
//		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
//		
//	}
	
//	@Override
//	protected void onStop() {
//		// TODO Auto-generated method stub
//		super.onStop();
//		//Unbind from MySyncService 
//		if(mBound){
//			unbindService(mConnection);
//			mBound=false; 
//		}
//	}
	
	

	
	private ServiceConnection mConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			System.out.println("Service is diconnected !!!");
			mBound= false;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			System.out.println("Service is connected !!!");

			//we've bound to MySyncService, cast the IBinder and get the LocalService instance 
			LocalBinder binder = (LocalBinder) service;
			sync_service  = binder.getService();
			mBound = true; 
		}
	};
	

//	/**
//	 * Method to check with the server in case there is internet connection 
//	 * 
//	 * @param url
//	 * @param ajax
//	 * @param c
//	 * @param version 
//	 */
//	public void async_post(String url, boolean ajax, boolean c,final int version) {
////	DialogHelper.showProcessingDialog(dialog, getString(R.string.login),
////			getString(R.string.wait));
//
//	try {
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put(Constant.TRACE_KEY, version);
//		AjaxCallback<String> cb = new AjaxCallback<String>() {
//			@Override
//			public void callback(String url, String json, AjaxStatus status) {
//				switch ((status.getCode())) {
//				case Constant.REQUEST_OK:  // request http success
//					// get the cookies set by spring security
//					cookies = status.getCookies();
//					int code = JsonHelper.getResponseCodeFromJson(json);
//					switch (code) {
//					case Constant.REQUEST_OK:
//
//						cookies = status.getCookies();
//						version = JsonHelper.getObjectFromJson(json,Portfolio.class, Constant.VERSION);
//						SharedPreferences.Editor edit = settings.edit();
//						edit.putString(Constant.USER, JsonHelper.createJsonObject(user).toString());
//						edit.putInt(Constant.VERSION, JsonHelper.createJsonObject(version).getInt(params));
//						
//						usernameStored = user.getUsername();
//						// Car, password cannot be read from server (because of Spring security: it is encrypted and not transmitted
//						passwordStored = password;
//						
//						for (Cookie cookie : cookies) {
//							if (cookie.getName().equals(Constant.SESSION_ID)) {
//								edit.putString("cookieName",cookie.getName());
//								edit.putString("cookieValue",cookie.getValue());
//								// edit.putLong("cookieExpiryDate",
//								// cookie.getExpiryDate().getTime());
//								edit.commit(); // apply
//							}
//						}
//						storeUser();
//						// set username and password into the applet at each connection
//						if (!hasRun) {
//							ctrl.execute("insert into pds_applet (username, password, key) values("+user.getUsername()+","+ password+",1234)", Constant.SET_LOGIN_INFO_CODE);
//						} else {
//							ctrl.execute("update pds_applet set (username, password) values("+user.getUsername()+","+ password+")", Constant.SET_LOGIN_INFO_CODE);
//						}
//						load_categories();
////						checkLoginInfo(username, password);
//						break;
//
//					case Constant.NOT_FOUND:
//						alert = new AlertDialog.Builder(LoginActivity.this);
//						DialogHelper.showErrorDialog(alert, getString(R.string.error),getString(R.string.wrong));							
//						break;
//					}
//					break;
//
//				default: // Request http failed
//
//					String msg = getString(R.string.code_404) + " "+ getString(R.string.request_not_received);
//					showErrorDialog(msg, 0);
//					break;
//				}
//				cookies = status.getCookies();
//			}
//		};
//		cb.header("X-Requested-With", "true");
//
//		aquery.progress(dialog).ajax(url, params, String.class, cb);
//
//	} catch (Exception e) {
//		e.printStackTrace(); // To change body of catch statement use File |
//	}
//}
//	


}
